home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / jpsrc2.zip / JRDGIF.C < prev    next >
C/C++ Source or Header  |  1991-12-12  |  21KB  |  621 lines

  1. /*
  2.  * jrdgif.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to read input images in GIF format.
  9.  *
  10.  * These routines may need modification for non-Unix environments or
  11.  * specialized applications.  As they stand, they assume input from
  12.  * an ordinary stdio stream.  They further assume that reading begins
  13.  * at the start of the file; input_init may need work if the
  14.  * user interface has already read some data (e.g., to determine that
  15.  * the file is indeed GIF format).
  16.  *
  17.  * These routines are invoked via the methods get_input_row
  18.  * and input_init/term.
  19.  */
  20.  
  21. /*
  22.  * This code is loosely based on giftoppm from the PBMPLUS distribution
  23.  * of Feb. 1991.  That file contains the following copyright notice:
  24.  * +-------------------------------------------------------------------+
  25.  * | Copyright 1990, David Koblas.                                     |
  26.  * |   Permission to use, copy, modify, and distribute this software   |
  27.  * |   and its documentation for any purpose and without fee is hereby |
  28.  * |   granted, provided that the above copyright notice appear in all |
  29.  * |   copies and that both that copyright notice and this permission  |
  30.  * |   notice appear in supporting documentation.  This software is    |
  31.  * |   provided "as is" without express or implied warranty.           |
  32.  * +-------------------------------------------------------------------+
  33.  *
  34.  * We are also required to state that
  35.  *    "The Graphics Interchange Format(c) is the Copyright property of
  36.  *    CompuServe Incorporated. GIF(sm) is a Service Mark property of
  37.  *    CompuServe Incorporated."
  38.  */
  39.  
  40. #include "jinclude.h"
  41.  
  42. #ifdef GIF_SUPPORTED
  43.  
  44.  
  45. #define    MAXCOLORMAPSIZE    256    /* max # of colors in a GIF colormap */
  46. #define NUMCOLORS    3    /* # of colors */
  47. #define CM_RED        0    /* color component numbers */
  48. #define CM_GREEN    1
  49. #define CM_BLUE        2
  50.  
  51. static JSAMPARRAY colormap;    /* the colormap to use */
  52. /* colormap[i][j] = value of i'th color component for pixel value j */
  53.  
  54. #define    MAX_LZW_BITS    12    /* maximum LZW code size */
  55. #define LZW_TABLE_SIZE    (1<<MAX_LZW_BITS) /* # of possible LZW symbols */
  56.  
  57. /* Macros for extracting header data --- note we assume chars may be signed */
  58.  
  59. #define LM_to_uint(a,b)        ((((b)&0xFF) << 8) | ((a)&0xFF))
  60.  
  61. #define BitSet(byte, bit)    ((byte) & (bit))
  62. #define INTERLACE    0x40    /* mask for bit signifying interlaced image */
  63. #define COLORMAPFLAG    0x80    /* mask for bit signifying colormap presence */
  64.  
  65. #define    ReadOK(file,buffer,len)    (FREAD(file,buffer,len) == ((size_t) (len)))
  66.  
  67. /* Static vars for GetCode and LZWReadByte */
  68.  
  69. static char code_buf[256+4];    /* current input data block */
  70. static int last_byte;        /* # of bytes in code_buf */
  71. static int last_bit;        /* # of bits in code_buf */
  72. static int cur_bit;        /* next bit index to read */
  73. static boolean out_of_blocks;    /* TRUE if hit terminator data block */
  74.  
  75. static int input_code_size;    /* codesize given in GIF file */
  76. static int clear_code,end_code; /* values for Clear and End codes */
  77.  
  78. static int code_size;        /* current actual code size */
  79. static int limit_code;        /* 2^code_size */
  80. static int max_code;        /* first unused code value */
  81. static boolean first_time;    /* flags first call to LZWReadByte */
  82.  
  83. /* LZW decompression tables:
  84.  *   symbol_head[K] = prefix symbol of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  85.  *   symbol_tail[K] = suffix byte   of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  86.  * Note that entries 0..end_code of the above tables are not used,
  87.  * since those symbols represent raw bytes or special codes.
  88.  *
  89.  * The stack represents the not-yet-used expansion of the last LZW symbol.
  90.  * In the worst case, a symbol could expand to as many bytes as there are
  91.  * LZW symbols, so we allocate LZW_TABLE_SIZE bytes for the stack.
  92.  * (This is conservative since that number includes the raw-byte symbols.)
  93.  *
  94.  * The tables are allocated from FAR heap space since they would use up
  95.  * rather a lot of the near data space in a PC.
  96.  */
  97.  
  98. static UINT16 FAR *symbol_head; /* => table of prefix symbols */
  99. static UINT8  FAR *symbol_tail; /* => table of suffix bytes */
  100. static UINT8  FAR *symbol_stack; /* stack for symbol expansions */
  101. static UINT8  FAR *sp;        /* stack pointer */
  102.  
  103. /* Static state for interlaced image processing */
  104.  
  105. static boolean is_interlaced;    /* TRUE if have interlaced image */
  106. static big_sarray_ptr interlaced_image;    /* full image in interlaced order */
  107. static long cur_row_number;    /* need to know actual row number */
  108. static long pass2_offset;    /* # of pixel rows in pass 1 */
  109. static long pass3_offset;    /* # of pixel rows in passes 1&2 */
  110. static long pass4_offset;    /* # of pixel rows in passes 1,2,3 */
  111.  
  112.  
  113. /* Forward declarations */
  114. METHODDEF void load_interlaced_image PP((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
  115. METHODDEF void get_interlaced_row PP((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
  116.  
  117.  
  118.  
  119. LOCAL int
  120. ReadByte (compress_info_ptr cinfo)
  121. /* Read next byte from GIF file */
  122. {
  123.   register FILE * infile = cinfo->input_file;
  124.   int c;
  125.  
  126.   if ((c = getc(infile)) == EOF)
  127.     ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  128.   return c;
  129. }
  130.  
  131.  
  132. LOCAL int
  133. GetDataBlock (compress_info_ptr cinfo, char *buf)
  134. /* Read a GIF data block, which has a leading count byte */
  135. /* A zero-length block marks the end of a data block sequence */
  136. {
  137.   int count;
  138.  
  139.   count = ReadByte(cinfo);
  140.   if (count > 0) {
  141.     if (! ReadOK(cinfo->input_file, buf, count))
  142.       ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  143.   }
  144.   return count;
  145. }
  146.  
  147.  
  148. LOCAL void
  149. SkipDataBlocks (compress_info_ptr cinfo)
  150. /* Skip a series of data blocks, until a block terminator is found */
  151. {
  152.   char buf[256];
  153.  
  154.   while (GetDataBlock(cinfo, buf) > 0)
  155.     /* skip */;
  156. }
  157.  
  158.  
  159. LOCAL void
  160. ReInitLZW (void)
  161. /* (Re)initialize LZW state; shared code for startup and Clear processing */
  162. {
  163.   code_size = input_code_size+1;
  164.   limit_code = clear_code << 1;    /* 2^code_size */
  165.   max_code = clear_code + 2;    /* first unused code value */
  166.   sp = symbol_stack;        /* init stack to empty */
  167. }
  168.  
  169.  
  170. LOCAL void
  171. InitLZWCode (void)
  172. /* Initialize for a series of LZWReadByte (and hence GetCode) calls */
  173. {
  174.   /* GetCode initialization */
  175.   last_byte = 2;        /* make safe to "recopy last two bytes" */
  176.   last_bit = 0;            /* nothing in the buffer */
  177.   cur_bit = 0;            /* force buffer load on first call */
  178.   out_of_blocks = FALSE;
  179.  
  180.   /* LZWReadByte initialization */
  181.   clear_code = 1 << input_code_size; /* compute special code values */
  182.   end_code = clear_code + 1;    /* note that these do not change */
  183.   first_time = TRUE;
  184.   ReInitLZW();
  185. }
  186.  
  187.  
  188. LOCAL int
  189. GetCode (compress_info_ptr cinfo)
  190. /* Fetch the next code_size bits from the GIF data */
  191. /* We assume code_size is less than 16 */
  192. {
  193.   register INT32 accum;
  194.   int offs, ret, count;
  195.  
  196.   if ( (cur_bit+code_size) > last_bit) {
  197.     /* Time to reload the buffer */
  198.     if (out_of_blocks) {
  199.       TRACEMS(cinfo->emethods, 1, "Ran out of GIF bits");
  200.       return end_code;        /* fake something useful */
  201.     }
  202.     /* preserve last two bytes of what we have -- assume code_size <= 16 */
  203.     code_buf[0] = code_buf[last_byte-2];
  204.     code_buf[1] = code_buf[last_byte-1];
  205.     /* Load more bytes; set flag if we reach the terminator block */
  206.     if ((count = GetDataBlock(cinfo, &code_buf[2])) == 0) {
  207.       out_of_blocks = TRUE;
  208.       TRACEMS(cinfo->emethods, 1, "Ran out of GIF bits");
  209.       return end_code;        /* fake something useful */
  210.     }
  211.     /* Reset counters */
  212.     cur_bit = (cur_bit - last_bit) + 16;
  213.     last_byte = 2 + count;
  214.     last_bit = last_byte * 8;
  215.   }
  216.  
  217.   /* Form up next 24 bits in accum */
  218.   offs = cur_bit >> 3;        /* byte containing cur_bit */
  219. #ifdef CHAR_IS_UNSIGNED
  220.   accum = code_buf[offs+2];
  221.   accum <<= 8;
  222.   accum |= code_buf[offs+1];
  223.   accum <<= 8;
  224.   accum |= code_buf[offs];
  225. #else
  226.   accum = code_buf[offs+2] & 0xFF;
  227.   accum <<= 8;
  228.   accum |= code_buf[offs+1] & 0xFF;
  229.   accum <<= 8;
  230.   accum |= code_buf[offs] & 0xFF;
  231. #endif
  232.  
  233.   /* Right-align cur_bit in accum, then mask off desired number of bits */
  234.   accum >>= (cur_bit & 7);
  235.   ret = ((int) accum) & ((1 << code_size) - 1);
  236.   
  237.   cur_bit += code_size;
  238.   return ret;
  239. }
  240.  
  241.  
  242. LOCAL int
  243. LZWReadByte (compress_info_ptr cinfo)
  244. /* Read an LZW-compressed byte */
  245. {
  246.   static int oldcode;        /* previous LZW symbol */
  247.   static int firstcode;        /* first byte of oldcode's expansion */
  248.   register int code;        /* current working code */
  249.   int incode;            /* saves actual input code */
  250.  
  251.   /* First time, just eat the expected Clear code(s) and return next code, */
  252.   /* which is assumed to be a raw byte. */
  253.   if (first_time) {
  254.     first_time = FALSE;
  255.     do {
  256.       code = GetCode(cinfo);
  257.     } while (code == clear_code);
  258.     firstcode = oldcode = code;    /* make firstcode, oldcode valid! */
  259.     return code;
  260.   }
  261.  
  262.   /* If any codes are stacked from a previously read symbol, return them */
  263.   if (sp > symbol_stack)
  264.     return (int) *(--sp);
  265.  
  266.   code = GetCode(cinfo);
  267.  
  268.   if (code == clear_code) {
  269.     /* Reinit static state, swallow any extra Clear codes, and return */
  270.     ReInitLZW();
  271.     do {
  272.       code = GetCode(cinfo);
  273.     } while (code == clear_code);
  274.     firstcode = oldcode = code; /* gotta reinit these too */
  275.     return code;
  276.   }
  277.  
  278.   if (code == end_code) {
  279.     /* Skip the rest of the image, unless GetCode already read terminator */
  280.     if (! out_of_blocks)
  281.       SkipDataBlocks(cinfo);
  282.     return -1;
  283.   }
  284.  
  285.   /* Normal raw byte or LZW symbol */
  286.   incode = code;        /* save for a moment */
  287.   
  288.   if (code >= max_code) {    /* special case for not-yet-defined symbol */
  289.     *sp++ = (UINT8) firstcode;    /* it will be defined as oldcode/firstcode */
  290.     code = oldcode;
  291.   }
  292.  
  293.   /* If it's a symbol, expand it into the stack */
  294.   while (code >= clear_code) {
  295.     *sp++ = symbol_tail[code];    /* tail of symbol: a simple byte value */
  296.     code = symbol_head[code];    /* head of symbol: another LZW symbol */
  297.   }
  298.   /* At this point code just represents a raw byte */
  299.   firstcode = code;        /* save for possible future use */
  300.  
  301.   /* If there's room in table, */
  302.   if ((code = max_code) < LZW_TABLE_SIZE) {
  303.     /* Define a new symbol = prev sym + head of this sym's expansion */
  304.     symbol_head[code] = oldcode;
  305.     symbol_tail[code] = (UINT8) firstcode;
  306.     max_code++;
  307.     /* Is it time to increase code_size? */
  308.     if ((max_code >= limit_code) && (code_size < MAX_LZW_BITS)) {
  309.       code_size++;
  310.       limit_code <<= 1;        /* keep equal to 2^code_size */
  311.     }
  312.   }
  313.   
  314.   oldcode = incode;        /* save last input symbol for future use */
  315.   return firstcode;        /* return first byte of symbol's expansion */
  316. }
  317.  
  318.  
  319. LOCAL void
  320. ReadColorMap (compress_info_ptr cinfo, int cmaplen, JSAMPARRAY cmap)
  321. /* Read a GIF colormap */
  322. {
  323.   int i;
  324.  
  325.   for (i = 0; i < cmaplen; i++) {
  326.     cmap[CM_RED][i]   = (JSAMPLE) ReadByte(cinfo);
  327.     cmap[CM_GREEN][i] = (JSAMPLE) ReadByte(cinfo);
  328.     cmap[CM_BLUE][i]  = (JSAMPLE) ReadByte(cinfo);
  329.   }
  330. }
  331.  
  332.  
  333. LOCAL void
  334. DoExtension (compress_info_ptr cinfo)
  335. /* Process an extension block */
  336. /* Currently we ignore 'em all */
  337. {
  338.   int extlabel;
  339.  
  340.   /* Read extension label byte */
  341.   extlabel = ReadByte(cinfo);
  342.   TRACEMS1(cinfo->emethods, 1, "Ignoring GIF extension block of type 0x%02x",
  343.        extlabel);
  344.   /* Skip the data block(s) associated with the extension */
  345.   SkipDataBlocks(cinfo);
  346. }
  347.  
  348.  
  349. /*
  350.  * Read the file header; return image size and component count.
  351.  */
  352.  
  353. METHODDEF void
  354. input_init (compress_info_ptr cinfo)
  355. {
  356.   char hdrbuf[10];        /* workspace for reading control blocks */
  357.   UINT16 width, height;        /* image dimensions */
  358.   int colormaplen, aspectRatio;
  359.   int c;
  360.  
  361.   /* Allocate space to store the colormap */
  362.   colormap = (*cinfo->emethods->alloc_small_sarray)
  363.         ((long) MAXCOLORMAPSIZE, (long) NUMCOLORS);
  364.  
  365.   /* Read and verify GIF Header */
  366.   if (! ReadOK(cinfo->input_file, hdrbuf, 6))
  367.     ERREXIT(cinfo->emethods, "Not a GIF file");
  368.   if (strncmp(hdrbuf, "GIF", 3) != 0)
  369.     ERREXIT(cinfo->emethods, "Not a GIF file");
  370.   /* Check for expected version numbers.
  371.    * If unknown version, give warning and try to process anyway;
  372.    * this is per recommendation in GIF89a standard.
  373.    */
  374.   if ((strncmp(hdrbuf+3, "87a", 3) != 0) &&
  375.       (strncmp(hdrbuf+3, "89a", 3) != 0))
  376.     TRACEMS3(cinfo->emethods, 1,
  377.          "Warning: unexpected GIF version number '%c%c%c'",
  378.          hdrbuf[3], hdrbuf[4], hdrbuf[5]);
  379.  
  380.   /* Read and decipher Logical Screen Descriptor */
  381.   if (! ReadOK(cinfo->input_file, hdrbuf, 7))
  382.     ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  383.   width = LM_to_uint(hdrbuf[0],hdrbuf[1]);
  384.   height = LM_to_uint(hdrbuf[2],hdrbuf[3]);
  385.   colormaplen = 2 << (hdrbuf[4] & 0x07);
  386.   /* we ignore the color resolution, sort flag, and background color index */
  387.   aspectRatio = hdrbuf[6] & 0xFF;
  388.   if (aspectRatio != 0 && aspectRatio != 49)
  389.     TRACEMS(cinfo->emethods, 1, "Warning: nonsquare pixels in input");
  390.  
  391.   /* Read global colormap if header indicates it is present */
  392.   if (BitSet(hdrbuf[4], COLORMAPFLAG))
  393.     ReadColorMap(cinfo, colormaplen, colormap);
  394.  
  395.   /* Scan until we reach start of desired image.
  396.    * We don't currently support skipping images, but could add it easily.
  397.    */
  398.   for (;;) {
  399.     c = ReadByte(cinfo);
  400.  
  401.     if (c == ';')        /* GIF terminator?? */
  402.       ERREXIT(cinfo->emethods, "Too few images in GIF file");
  403.  
  404.     if (c == '!') {        /* Extension */
  405.       DoExtension(cinfo);
  406.       continue;
  407.     }
  408.     
  409.     if (c != ',') {        /* Not an image separator? */
  410.       TRACEMS1(cinfo->emethods, 1, "Bogus input char 0x%02x, ignoring", c);
  411.       continue;
  412.     }
  413.  
  414.     /* Read and decipher Local Image Descriptor */
  415.     if (! ReadOK(cinfo->input_file, hdrbuf, 9))
  416.       ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  417.     /* we ignore top/left position info, also sort flag */
  418.     width = LM_to_uint(hdrbuf[4],hdrbuf[5]);
  419.     height = LM_to_uint(hdrbuf[6],hdrbuf[7]);
  420.     is_interlaced = BitSet(hdrbuf[8], INTERLACE);
  421.     colormaplen = 2 << (hdrbuf[8] & 0x07);
  422.  
  423.     /* Read local colormap if header indicates it is present */
  424.     /* Note: if we wanted to support skipping images, */
  425.     /* we'd need to skip rather than read colormap for ignored images */
  426.     if (BitSet(hdrbuf[8], COLORMAPFLAG))
  427.       ReadColorMap(cinfo, colormaplen, colormap);
  428.  
  429.     input_code_size = ReadByte(cinfo); /* get minimum-code-size byte */
  430.     if (input_code_size < 2 || input_code_size >= MAX_LZW_BITS)
  431.       ERREXIT1(cinfo->emethods, "Bogus codesize %d", input_code_size);
  432.  
  433.     /* Reached desired image, so break out of loop */
  434.     /* If we wanted to skip this image, */
  435.     /* we'd call SkipDataBlocks and then continue the loop */
  436.     break;
  437.   }
  438.  
  439.   /* Prepare to read selected image: first initialize LZW decompressor */
  440.   symbol_head = (UINT16 FAR *) (*cinfo->emethods->alloc_medium)
  441.                 (LZW_TABLE_SIZE * SIZEOF(UINT16));
  442.   symbol_tail = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
  443.                 (LZW_TABLE_SIZE * SIZEOF(UINT8));
  444.   symbol_stack = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
  445.                 (LZW_TABLE_SIZE * SIZEOF(UINT8));
  446.   InitLZWCode();
  447.  
  448.   /*
  449.    * If image is interlaced, we read it into a full-size sample array,
  450.    * decompressing as we go; then get_input_row selects rows from the
  451.    * sample array in the proper order.
  452.    */
  453.   if (is_interlaced) {
  454.     /* We request the big array now, but can't access it until the pipeline
  455.      * controller causes all the big arrays to be allocated.  Hence, the
  456.      * actual work of reading the image is postponed until the first call
  457.      * of get_input_row.
  458.      */
  459.     interlaced_image = (*cinfo->emethods->request_big_sarray)
  460.         ((long) width, (long) height, 1L);
  461.     cinfo->methods->get_input_row = load_interlaced_image;
  462.   }
  463.  
  464.   /* Return info about the image. */
  465.   cinfo->input_components = NUMCOLORS;
  466.   cinfo->in_color_space = CS_RGB;
  467.   cinfo->image_width = width;
  468.   cinfo->image_height = height;
  469.   cinfo->data_precision = 8;
  470. }
  471.  
  472.  
  473. /*
  474.  * Read one row of pixels.
  475.  * This version is used for noninterlaced GIF images:
  476.  * we read directly from the GIF file.
  477.  */
  478.  
  479. METHODDEF void
  480. get_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  481. {
  482.   register JSAMPROW ptr0, ptr1, ptr2;
  483.   register long col;
  484.   register int c;
  485.   
  486.   ptr0 = pixel_row[0];
  487.   ptr1 = pixel_row[1];
  488.   ptr2 = pixel_row[2];
  489.   for (col = cinfo->image_width; col > 0; col--) {
  490.     if ((c = LZWReadByte(cinfo)) < 0)
  491.       ERREXIT(cinfo->emethods, "Premature end of GIF image");
  492.     *ptr0++ = colormap[CM_RED][c];
  493.     *ptr1++ = colormap[CM_GREEN][c];
  494.     *ptr2++ = colormap[CM_BLUE][c];
  495.   }
  496. }
  497.  
  498.  
  499. /*
  500.  * Read one row of pixels.
  501.  * This version is used for the first call on get_input_row when
  502.  * reading an interlaced GIF file: we read the whole image into memory.
  503.  */
  504.  
  505. METHODDEF void
  506. load_interlaced_image (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  507. {
  508.   JSAMPARRAY image_ptr;
  509.   register JSAMPROW sptr;
  510.   register long col;
  511.   register int c;
  512.   long row;
  513.  
  514.   /* Read the interlaced image into the big array we've created. */
  515.   for (row = 0; row < cinfo->image_height; row++) {
  516.     image_ptr = (*cinfo->emethods->access_big_sarray)
  517.             (interlaced_image, row, TRUE);
  518.     sptr = image_ptr[0];
  519.     for (col = cinfo->image_width; col > 0; col--) {
  520.       if ((c = LZWReadByte(cinfo)) < 0)
  521.     ERREXIT(cinfo->emethods, "Premature end of GIF image");
  522.       *sptr++ = (JSAMPLE) c;
  523.     }
  524.   }
  525.  
  526.   /* Replace method pointer so subsequent calls don't come here. */
  527.   cinfo->methods->get_input_row = get_interlaced_row;
  528.   /* Initialize for get_interlaced_row, and perform first call on it. */
  529.   cur_row_number = 0;
  530.   pass2_offset = (cinfo->image_height + 7L) / 8L;
  531.   pass3_offset = pass2_offset + (cinfo->image_height + 3L) / 8L;
  532.   pass4_offset = pass3_offset + (cinfo->image_height + 1L) / 4L;
  533.  
  534.   get_interlaced_row(cinfo, pixel_row);
  535. }
  536.  
  537.  
  538. /*
  539.  * Read one row of pixels.
  540.  * This version is used for interlaced GIF images:
  541.  * we read from the big in-memory image.
  542.  */
  543.  
  544. METHODDEF void
  545. get_interlaced_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  546. {
  547.   JSAMPARRAY image_ptr;
  548.   register JSAMPROW sptr, ptr0, ptr1, ptr2;
  549.   register long col;
  550.   register int c;
  551.   long irow;
  552.  
  553.   /* Figure out which row of interlaced image is needed, and access it. */
  554.   switch ((int) (cur_row_number & 7L)) {
  555.   case 0:            /* first-pass row */
  556.     irow = cur_row_number >> 3;
  557.     break;
  558.   case 4:            /* second-pass row */
  559.     irow = (cur_row_number >> 3) + pass2_offset;
  560.     break;
  561.   case 2:            /* third-pass row */
  562.   case 6:
  563.     irow = (cur_row_number >> 2) + pass3_offset;
  564.     break;
  565.   default:            /* fourth-pass row */
  566.     irow = (cur_row_number >> 1) + pass4_offset;
  567.     break;
  568.   }
  569.   image_ptr = (*cinfo->emethods->access_big_sarray)
  570.             (interlaced_image, irow, FALSE);
  571.   /* Scan the row, expand colormap, and output */
  572.   sptr = image_ptr[0];
  573.   ptr0 = pixel_row[0];
  574.   ptr1 = pixel_row[1];
  575.   ptr2 = pixel_row[2];
  576.   for (col = cinfo->image_width; col > 0; col--) {
  577.     c = GETJSAMPLE(*sptr++);
  578.     *ptr0++ = colormap[CM_RED][c];
  579.     *ptr1++ = colormap[CM_GREEN][c];
  580.     *ptr2++ = colormap[CM_BLUE][c];
  581.   }
  582.   cur_row_number++;        /* for next time */
  583. }
  584.  
  585.  
  586. /*
  587.  * Finish up at the end of the file.
  588.  */
  589.  
  590. METHODDEF void
  591. input_term (compress_info_ptr cinfo)
  592. {
  593.   if (is_interlaced) {
  594.     (*cinfo->emethods->free_big_sarray) (interlaced_image);
  595.   }
  596.   (*cinfo->emethods->free_small_sarray)
  597.         (colormap, (long) NUMCOLORS);
  598.   (*cinfo->emethods->free_medium) ((void FAR *) symbol_head);
  599.   (*cinfo->emethods->free_medium) ((void FAR *) symbol_tail);
  600.   (*cinfo->emethods->free_medium) ((void FAR *) symbol_stack);
  601. }
  602.  
  603.  
  604. /*
  605.  * The method selection routine for GIF format input.
  606.  * Note that this must be called by the user interface before calling
  607.  * jpeg_compress.  If multiple input formats are supported, the
  608.  * user interface is responsible for discovering the file format and
  609.  * calling the appropriate method selection routine.
  610.  */
  611.  
  612. GLOBAL void
  613. jselrgif (compress_info_ptr cinfo)
  614. {
  615.   cinfo->methods->input_init = input_init;
  616.   cinfo->methods->get_input_row = get_input_row; /* assume uninterlaced */
  617.   cinfo->methods->input_term = input_term;
  618. }
  619.  
  620. #endif /* GIF_SUPPORTED */
  621.